shared float4x4 ViewProj;
shared float2 halfPixel;

shared texture DepthRT;
shared texture DiffuseRT;
shared texture NormalRT;

float4 LightEquation(float3 lightPosition, float4 pixelPosition, float lightRadius,
                     float3 normal, float3 lightColor, float3 cameraPosition,
                     float specularIntensity, float specularPower, float lightIntensity)
{
	float3 lightVector = lightPosition - pixelPosition;
	float distance = length(lightVector);
	float attenuation = smoothstep(0.0f, 1.0f, 1.0f - length(lightVector)/lightRadius); 
	
	lightVector = normalize(lightVector);
	
	float NdL = max(0, dot(normal, lightVector));
	float3 diffuseLight = NdL * lightColor;
	
	float3 reflectionVector = normalize(reflect(-lightVector, normal));
	float3 directionToCamera = normalize(cameraPosition - pixelPosition);
	float specularLight = specularIntensity * pow(saturate(dot(reflectionVector, directionToCamera)), specularPower);
	
	return attenuation * lightIntensity * float4(diffuseLight, specularLight);
}

void FillVariables(float4 screenPosition, sampler normalSampler, sampler diffuseSampler,
                   sampler depthSampler, float4x4 InverseViewProj, out float3 normal,
                   out float specularPower, out float specularIntensity, out float4 position)
{
	screenPosition.xy /= screenPosition.w;
	float2 texCoord = 0.5f * (float2(screenPosition.x, -screenPosition.y) + 1.0f);
	texCoord -= halfPixel;
	
	float4 normalData = tex2D(normalSampler, texCoord);
	normal = 2.0f * normalData.xyz - 1.0f;
	specularPower = normalData.a * 255.0f;
	specularIntensity = tex2D(diffuseSampler, texCoord).a;
	
	float depthVal = tex2D(depthSampler, texCoord).r;
	
	position.xy = screenPosition.xy;
	position.z = depthVal;
	position.w = 1.0f;
	position = mul(position, InverseViewProj);
	position.xyzw /= position.w;
}